package com.igoal.web;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.igoal.comparator.GoalEntryComparator;
import com.igoal.dto.UserDTO;
import com.igoal.service.GoalEntryManager;
import com.igoal.service.GoalManager;
import com.igoal.service.MessageManager;
import com.igoal.service.UserGoalManager;
import com.igoal.service.UserManagerExt;
import com.igoal.service.paging.PageItem;
import com.igoal.service.paging.PagingService;
import com.igoal.web.model.BaseModel;
import com.igoal.web.model.UserDashboardModel;

/**
 * User-Dashboard controller
 * 
 * @author Tri
 * @since 6/19/2010
 * 
 */
@Controller
@RequestMapping("/dashboard.html")
public class UserDashboardController extends BaseController {
	@Autowired
	public void setUserManagerExt(UserManagerExt userManagerExt) {
		this.userManagerExt = userManagerExt;
	}

	@Autowired
	public void setGoalManager(GoalManager goalManager) {
		this.goalManager = goalManager;
	}

	@Autowired
	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}

	@Autowired
	public void setUserGoalManager(UserGoalManager userGoalManager) {
		this.userGoalManager = userGoalManager;
	}

	@Autowired
	public void setGoalEntryManager(GoalEntryManager goalEnryManager) {
		this.goalEntryManager = goalEnryManager;
	}
	
	public String getViewPrefix() {
		return BaseControllerDelegate.DEFAULT_VIEW_PREFIX;
	}

	@RequestMapping(method = RequestMethod.GET)
	public String showUserDashboard(ModelMap model, HttpServletRequest request) {
		UserDTO user = getUserSession();

		initModel(model, user, request);
		return getViewPrefix() + "userDashboard";
	}

	private void initModel(ModelMap modelMap, UserDTO user, HttpServletRequest request) {
		if (modelMap == null || user == null) {
			return;
		}

		UserDashboardModel model = new UserDashboardModel();
////		simpleModelInitialize(user, model);
//
//		// top zone
//		model.setUserId(user.getId());
//		
//		// left zone
//		model.setDoingGoals(userGoalManager.getDoingGoal(user.getId()));
//		model.setCompletedGoals(userGoalManager.getCompletedGoal(user.getId()));
//		model.setShouldDoGoals(goalManager.getShouldDoGoals(user.getId()));
//		List<PageItem<Long>> recentEntries = goalEntryManager.getRecentEntries(user, user);
//		Collections.sort(recentEntries, new GoalEntryComparator(false));
//
//		List<PageItem<Long>> result = new ArrayList<PageItem<Long>>();
//
//		for (int index = 0; index < PagingService.MAX_ENTRIES
//				&& index < recentEntries.size(); index++) {
//			result.add(recentEntries.get(index));
//		}
//
//		model.setRecentEntries(result);
//
//		List<GoalDTO> topGoals = goalManager.getTopGoals(user);
//		model.setTopGoals(WebUtils.buildGoalCloud(topGoals));
//
//		List<GoalDTO> commonGoals = goalManager.getCommonGoals(user);
//		model.setCommonGoals(WebUtils.buildGoalCloud(commonGoals));
//
//		model.getSettings().setAllowAddGoal(userGoalManager.allowCreateNewGoal(user));
//		
//		//right zone
//		initPeopleYouMayKnow(user, model);
		
		initTopPage(model, request);
		initRightPage(model, request);
		initLeftPage(model, request);
		
		applyModel(modelMap, model);
	}

	public void initTopPage(BaseModel model, HttpServletRequest request) {
		UserDTO user = getUserSession();
		initTopZone(user, model);
		model.setUserId(user.getId());
	}

	public void initRightPage(BaseModel model, HttpServletRequest request) {
		UserDTO user = getUserSession();
		initRightZone(user, model);
		initPeopleYouMayKnow(user, model);
	}

	@SuppressWarnings("unchecked")
	public void initLeftPage(BaseModel baseModel, HttpServletRequest request) {
		UserDTO user = getUserSession();
		UserDashboardModel model = (UserDashboardModel) baseModel;
		model.setDoingGoals(userGoalManager.getDoingGoal(user.getId()));
		model.setCompletedGoals(userGoalManager.getCompletedGoal(user.getId()));
		model.setShouldDoGoals(goalManager.getShouldDoGoals(user.getId()));
		List<PageItem<Long>> recentEntries = goalEntryManager.getRecentEntries(user, user);
		Collections.sort(recentEntries, new GoalEntryComparator(false));

		List<PageItem<Long>> result = new ArrayList<PageItem<Long>>();

		for (int index = 0; index < PagingService.MAX_ENTRIES
				&& index < recentEntries.size(); index++) {
			result.add(recentEntries.get(index));
		}

		model.setRecentEntries(result);		

		model.getSettings().setAllowAddGoal(userGoalManager.allowCreateNewGoal(user));
	}

}
